﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using System.IO;
using Bitis_KeToanThue_Pro_DungChung.Common;
using Bitis_KeToanThue_Pro_QuanTri.AccessDatabase;

namespace Bitis_KeToanThue_Pro_QuanTri.Form
{
    public partial class frmEditQuyenInGroupOnUser : Telerik.WinControls.UI.RadForm
    {
        EditQuyenInGroupOnUser cs = new EditQuyenInGroupOnUser();
        public frmEditQuyenInGroupOnUser()
        {
            InitializeComponent();
            khoitaogridnhomnguoidung();
            khoitaogridnguoidung();
            khoitaogrid();
            loadluoinhomnguoidung();
           
            ClassXuLyChung.loadcombodungchung(cbNhomMenuNguoiDung, cs.loadcombonhommenu(), "TenNhomMenuND", "MaNhomMenuND");
            ClassXuLyChung.loadcombodungchung(cbChuongTrinh, cs.loadcombochuongtrinh(), "TenChuongTrinh", "MaChuongTrinh");
            ClassXuLyChung.loadcombodungchung(cbViTri, cs.loadcombovitri(), "DienGiai", "IDViTri");
            UpdatePanelInfo(this.radGridView2.CurrentRow);
          /*  loadgrid(ClassXuLyChung.GetSafeInt(radGridView1.CurrentRow.Cells[0].Value),ClassXuLyChung.GetSafeInt(cbViTri.SelectedValue),
                ClassXuLyChung.GetSafeInt(cbChuongTrinh.SelectedValue), ClassXuLyChung.GetSafeInt(cbNhomMenuNguoiDung.SelectedValue));*/
            if (BienHeThong.ht_quyencapnhat == 1)
            {
                btnCapNhat.Enabled = true;
            }
            else
            {
                btnCapNhat.Enabled = false;
            }
        }
        private void loadgrid(int idphannhomnguoidung, int vitri, int chuongtrinh, int nhommenu)
        {
            this.radGridView3.DataSource = cs.loadluoieditquyen(idphannhomnguoidung, vitri, chuongtrinh, nhommenu);
        }
        private void khoitaogrid()
        {
            this.radGridView3.EnableHotTracking = true;
            this.radGridView3.ShowFilteringRow = false;
            this.radGridView3.EnableFiltering = true;
            this.radGridView3.MasterTemplate.ShowHeaderCellButtons = true;
            this.radGridView3.MasterTemplate.ShowFilteringRow = false;
            this.radGridView3.MasterTemplate.AutoExpandGroups = true;
           // this.radGridView3.TableElement.RowHeight = 32;
            this.radGridView3.MasterTemplate.AllowAddNewRow = false;
            this.radGridView3.MasterTemplate.AutoGenerateColumns = false;
            this.radGridView3.MasterTemplate.AllowEditRow = true;
            this.radGridView3.MasterTemplate.AllowDeleteRow = false;
            this.radGridView3.MasterTemplate.EnableSorting = true;
            this.radGridView3.MasterTemplate.AllowAutoSizeColumns = true;
            this.radGridView3.AutoScroll = true;
            this.radGridView3.EnableAlternatingRowColor = true;

     

            GridViewTextBoxColumn textBoxColumn7 = new GridViewTextBoxColumn()
            {
                FieldName = "ID",
                Name = "ID",
                HeaderText = "ID",
                Width = 107,
                IsVisible = false,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView3.Columns.Add(textBoxColumn7);

            GridViewTextBoxColumn textBoxColumn = new GridViewTextBoxColumn()
            {
                FieldName = "MenuID",
                Name = "MenuID",
                HeaderText = "ID",
                Width = 40,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView3.Columns.Add(textBoxColumn);

            GridViewTextBoxColumn textBoxColumn1 = new GridViewTextBoxColumn()
            {
                FieldName = "TenMenu",
                Name = "TenMenu",
                HeaderText = "Tên Menu",
                Width = 220,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView3.Columns.Add(textBoxColumn1);



          

          




            GridViewTextBoxColumn textBoxColumn9 = new GridViewTextBoxColumn()
            {
                FieldName = "MenuCha",
                Name = "MenuCha",
                HeaderText = "Menu Cha",
                Width = 70,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView3.Columns.Add(textBoxColumn9);

            GridViewTextBoxColumn textBoxColumn10 = new GridViewTextBoxColumn()
            {
                FieldName = "Cap",
                Name = "Cap",
                HeaderText = "Cấp",
                Width = 40,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView3.Columns.Add(textBoxColumn10);

           


            CustomCheckBoxColumn checkBoxColumn1 = new CustomCheckBoxColumn()
            {
                HeaderText = "",
                FieldName = "Quyen_CapNhat",
                Name = "Quyen_CapNhat",
                Width = 60,
                IsVisible = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView3.Columns.Add(checkBoxColumn1);


            CustomCheckBoxColumn checkBoxColumn2 = new CustomCheckBoxColumn()
            {
                HeaderText = "",
                FieldName = "Quyen_Xem",
                Name = "Quyen_Xem",
                Width = 60,
                IsVisible = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView3.Columns.Add(checkBoxColumn2);

            CustomCheckBoxColumn checkBoxColumn3 = new CustomCheckBoxColumn()
            {
                HeaderText = "",
                FieldName = "Quyen_Xoa",
                Name = "Quyen_Xoa",
                Width = 60,
                IsVisible = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView3.Columns.Add(checkBoxColumn3);

        

            ColumnGroupsViewDefinition def = new ColumnGroupsViewDefinition();

            def.ColumnGroups.Add(new GridViewColumnGroup("Đơn Vị Kiểm Tra"));
            def.ColumnGroups[0].ShowHeader = false;
            def.ColumnGroups[0].Rows.Add(new GridViewColumnGroupRow());
            def.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView3.Columns[0]);
            def.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView3.Columns[1]);
            def.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView3.Columns[2]);
            def.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView3.Columns[3]);
            def.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView3.Columns[4]);
          

            def.ColumnGroups.Add(new GridViewColumnGroup("Cập Nhật"));
            def.ColumnGroups[1].ShowHeader = true;
            def.ColumnGroups[1].Rows.Add(new GridViewColumnGroupRow());
            def.ColumnGroups[1].Rows[0].Columns.Add(this.radGridView3.Columns[5]);

            def.ColumnGroups.Add(new GridViewColumnGroup("Xem"));
            def.ColumnGroups[2].ShowHeader = true;
            def.ColumnGroups[2].Rows.Add(new GridViewColumnGroupRow());
            def.ColumnGroups[2].Rows[0].Columns.Add(this.radGridView3.Columns[6]);

            def.ColumnGroups.Add(new GridViewColumnGroup("Xóa"));
            def.ColumnGroups[3].ShowHeader = true;
            def.ColumnGroups[3].Rows.Add(new GridViewColumnGroupRow());
            def.ColumnGroups[3].Rows[0].Columns.Add(this.radGridView3.Columns[7]);
            this.radGridView3.ViewDefinition = def;

       
        }
        private void UpdatePanelInfoLoad(GridViewRowInfo currentRow)
        {
            if (currentRow != null && !(currentRow is GridViewNewRowInfo))
            {


                loadgrid(ClassXuLyChung.GetSafeInt(currentRow.Cells[0].Value), ClassXuLyChung.GetSafeInt(cbViTri.SelectedValue),
              ClassXuLyChung.GetSafeInt(cbChuongTrinh.SelectedValue), ClassXuLyChung.GetSafeInt(cbNhomMenuNguoiDung.SelectedValue));
            }
            else
            {
                loadgrid(0,0,0,0);
            }
        }
        private void UpdatePanelInfo(GridViewRowInfo currentRow)
        {
            if (currentRow != null && !(currentRow is GridViewNewRowInfo))
            {
             

                loadluoinguoidung(ClassXuLyChung.GetSafeInt(currentRow.Cells[0].Value));
                UpdatePanelInfoLoad(radGridView1.CurrentRow);
            }
            else
            {
             loadluoinguoidung(0);
            }
        }
        private void loadluoinguoidung(int manhom)
        {
            this.radGridView1.DataSource = cs.loadluoiuser(manhom);
        }
        private void khoitaogridnguoidung()
        {
            this.radGridView1.EnableHotTracking = true;
            this.radGridView1.ShowFilteringRow = false;
            this.radGridView1.EnableFiltering = true;
            this.radGridView1.MasterTemplate.ShowHeaderCellButtons = true;
            this.radGridView1.MasterTemplate.ShowFilteringRow = false;
            this.radGridView1.MasterTemplate.AutoExpandGroups = true;
            //this.radGridView1.TableElement.RowHeight = 65;
            this.radGridView1.MasterTemplate.AllowAddNewRow = false;
            this.radGridView1.MasterTemplate.AutoGenerateColumns = false;
            this.radGridView1.MasterTemplate.AllowEditRow = true;
            this.radGridView1.MasterTemplate.AllowDeleteRow = false;
            this.radGridView1.MasterTemplate.EnableSorting = true;
            this.radGridView1.MasterTemplate.AllowAutoSizeColumns = true;
            this.radGridView1.AutoScroll = true;
            this.radGridView1.EnableAlternatingRowColor = true;


            GridViewTextBoxColumn textBoxColumn0 = new GridViewTextBoxColumn()
            {
                FieldName = "ID",
                Name = "ID",
                HeaderText = "ID",
                Width = 50,
                IsVisible = false,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn0);

            GridViewTextBoxColumn textBoxColumn = new GridViewTextBoxColumn()
            {
                FieldName = "UserID",
                Name = "UserID",
                HeaderText = "ID",
                Width = 50,
                IsVisible = false,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn);

            GridViewTextBoxColumn textBoxColumn1 = new GridViewTextBoxColumn()
            {
                FieldName = "MaNV",
                Name = "MaNV",
                HeaderText = "Mã NV",
                Width = 70,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn1);

            GridViewTextBoxColumn textBoxColumn2 = new GridViewTextBoxColumn()
            {
                FieldName = "TenNV",
                Name = "TenNV",
                HeaderText = "Tên Nhân Viên",
                Width = 178,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn2);

            GridViewTextBoxColumn textBoxColumn3 = new GridViewTextBoxColumn()
            {
                FieldName = "TenPB",
                Name = "TenPB",
                HeaderText = "Phòng Ban",
                Width = 178,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn3);


         
        }
        private void loadluoinhomnguoidung()
        {
            this.radGridView2.DataSource =cs.loadluoiusergroup();
        }
        private void khoitaogridnhomnguoidung()
        {
            this.radGridView2.EnableHotTracking = true;
            this.radGridView2.ShowFilteringRow = false;
            this.radGridView2.EnableFiltering = true;
            this.radGridView2.MasterTemplate.ShowHeaderCellButtons = true;
            this.radGridView2.MasterTemplate.ShowFilteringRow = false;
            this.radGridView2.MasterTemplate.AutoExpandGroups = true;
            //this.radGridView1.TableElement.RowHeight = 65;
            this.radGridView2.MasterTemplate.AllowAddNewRow = false;
            this.radGridView2.MasterTemplate.AutoGenerateColumns = false;
            this.radGridView2.MasterTemplate.AllowEditRow = true;
            this.radGridView2.MasterTemplate.AllowDeleteRow = false;
            this.radGridView2.MasterTemplate.EnableSorting = true;
            this.radGridView2.MasterTemplate.AllowAutoSizeColumns = true;
            this.radGridView2.AutoScroll = true;
            this.radGridView2.EnableAlternatingRowColor = true;

            GridViewTextBoxColumn textBoxColumn = new GridViewTextBoxColumn()
            {
                FieldName = "MaNhomUser",
                Name = "MaNhomUser",
                HeaderText = "ID",
                Width = 50,
                IsVisible = false,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView2.Columns.Add(textBoxColumn);

            GridViewTextBoxColumn textBoxColumn1 = new GridViewTextBoxColumn()
            {
                FieldName = "TenNhomUser",
                Name = "TenNhomUser",
                HeaderText = "Tên Nhóm",
                Width = 220,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView2.Columns.Add(textBoxColumn1);



        }
        private void frmEditQuyenInGroupOnUser_Load(object sender, EventArgs e)
        {

        }

        private void txtTimKiemGroup_TextChanged(object sender, EventArgs e)
        {
            ClassXuLyChung.tuychontimkiemtrenluoi(txtTimKiemGroup, radGridView2, 0, 0);
        }

        private void txtTimKiemUser_TextChanged(object sender, EventArgs e)
        {
            ClassXuLyChung.tuychontimkiemtrenluoi(txtTimKiemUser, radGridView1, 0, 0);
        }

        private void radGridView2_CellClick(object sender, GridViewCellEventArgs e)
        {
            UpdatePanelInfo(this.radGridView2.CurrentRow);
        }

        private void radGridView1_CellClick(object sender, GridViewCellEventArgs e)
        {
            UpdatePanelInfoLoad(radGridView1.CurrentRow);
        }

        private void radTextBox1_TextChanged(object sender, EventArgs e)
        {
            ClassXuLyChung.tuychontimkiemtrenluoi(radTextBox1,radGridView3,0,3);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            int checkdk = 0;

            DataTable dtChanges = new DataTable();
            DataTable dt = (DataTable)radGridView3.DataSource;
            dtChanges = dt.GetChanges(DataRowState.Modified);

            // dtChanges = dt.GetChanges();

            if (dtChanges != null)
            {
                for (int i = 0; i < dtChanges.Rows.Count; i++)
                {

                    int capnhat_u = ClassXuLyChung.GetSafeBoolInt(dtChanges.Rows[i]["Quyen_CapNhat"]);
                    int xem_u = ClassXuLyChung.GetSafeBoolInt(dtChanges.Rows[i]["Quyen_Xem"]);
                    int xoa_u = ClassXuLyChung.GetSafeBoolInt(dtChanges.Rows[i]["Quyen_Xoa"]);

                    int idupdate = ClassXuLyChung.GetSafeInt(dtChanges.Rows[i]["ID"]);


                    try
                    {
                        cs.capnhatquyen(idupdate, capnhat_u, xem_u, xoa_u);


                    }
                    catch (Exception ex)
                    {
                        RadMessageBox.Show("Lỗi :" + ex.ToString(), "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Error);
                        checkdk = 1;
                    }

                }
                if (checkdk == 0)
                {
                    RadMessageBox.Show("Cập Nhật Thành Công", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Info);
                }
                checkdk = 0;
                dtChanges = null;
            }


            UpdatePanelInfoLoad(radGridView1.CurrentRow);
        }

      
    }
}
